﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace VPLink2013.Batch
{
    public class TeamFromOrganisationList
    {
        public string DirectionName
        {
            get;
            set;
        }
        public string DepartementName
        {
            get;
            set;
        }
        public string ServcieName
        {
            get;
            set;
        }
        public string CodeTeam
        {
            get;
            set;
        }
    }
    
    public class SearchTeams
    {
        private string _urlSite;

        public SearchTeams(string url)
        {
            _urlSite = url;
        }

        public List<TeamFromOrganisationList> GetAllTeamsByPrefix(string teamPrefix)
        {
            using (SPSite site = new SPSite(_urlSite))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList orgaList = web.GetList(Constants.OrganisationListUrl);
                    if (orgaList != null)
                    {
                        SPListItemCollection res = orgaList.GetSPListItemCollectionWithTextFields(new List<string> 
						{ Constants.OrganisationDirectionField,
							Constants.OrganisationDepartementField,
							Constants.OrganisationServiceField,
							Constants.OrganisationCodeField
						});

                        return (from x in res.Cast<SPListItem>()
                                where orgaList.GetItemListFieldValueAsText(Constants.OrganisationCodeField, x).StartsWith(teamPrefix)
                                select new TeamFromOrganisationList
                                {
                                    CodeTeam = orgaList.GetItemListFieldValueAsText(Constants.OrganisationCodeField, x),
                                    DirectionName = orgaList.GetItemListFieldValueAsText(Constants.OrganisationDirectionField, x),
                                    DepartementName = orgaList.GetItemListFieldValueAsText(Constants.OrganisationDepartementField, x),
                                    ServcieName = orgaList.GetItemListFieldValueAsText(Constants.OrganisationServiceField, x)
                                }).Distinct().ToList();
                    }
                    else return null;
                }
            }
        }

        public List<TeamFromOrganisationList> GetAllTeamsByDirection(string fieldTitle, string teamName)
        {
            using (SPSite site = new SPSite(_urlSite))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList orgaList = web.GetList(Constants.OrganisationListUrl);
                    if (orgaList != null)
                    {
                        SPListItemCollection res = orgaList.GetSPListItemCollectionWithTextFields(new List<string> 
						{ Constants.OrganisationDirectionField,
							Constants.OrganisationDepartementField,
							Constants.OrganisationServiceField,
							Constants.OrganisationCodeField
						});

                        return (from x in res.Cast<SPListItem>()
                                where teamName.Equals(orgaList.GetItemListFieldValueAsText(fieldTitle, x))
                                //where orgaList.GetItemListFieldValueAsText( Constants.OrganisationCodeField, x ).StartsWith(Constants.LDAP_DPT_CODE_Prefix )
                                select new TeamFromOrganisationList
                                {
                                    CodeTeam = orgaList.GetItemListFieldValueAsText(Constants.OrganisationCodeField, x),
                                    DirectionName = orgaList.GetItemListFieldValueAsText(Constants.OrganisationDirectionField, x),
                                    DepartementName = orgaList.GetItemListFieldValueAsText(Constants.OrganisationDepartementField, x),
                                    ServcieName = orgaList.GetItemListFieldValueAsText(Constants.OrganisationServiceField, x)
                                }).Distinct().ToList();
                    }
                    else return null;
                }
            }
        }
        public bool DoesDepartementHaveServices(string departementName)
        {
            bool foundSubDir = false;
            using (SPSite site = new SPSite(_urlSite))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList orgaList = web.GetList(Constants.OrganisationListUrl);
                    if (orgaList != null)
                    {
                        SPListItemCollection res = orgaList.GetSPListItemCollectionWithTextFields(new List<string> 
						{ Constants.OrganisationDirectionField,
							Constants.OrganisationDepartementField,
							Constants.OrganisationServiceField,
							Constants.OrganisationCodeField
						});
                        int nbSubDir = (from item in res.Cast<SPListItem>()
                                        where departementName.Equals(orgaList.GetItemListFieldValueAsText(Constants.OrganisationDepartementField, item), StringComparison.CurrentCultureIgnoreCase)
                                        select item.ID).Count();
                        foundSubDir = nbSubDir > 1 ? true : false;
                    }
                }
            }
            return foundSubDir;
        }

        public bool DoesTeamHaveSubDirection(string teamName, string organisationField)
        {
            bool foundSubDir = false;
            using (SPSite site = new SPSite(_urlSite))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList orgaList = web.GetList(Constants.OrganisationListUrl);
                    if (orgaList != null)
                    {
                        SPListItemCollection res = orgaList.GetSPListItemCollectionWithTextFields(new List<string> 
						{ Constants.OrganisationDirectionField,
							Constants.OrganisationDepartementField,
							Constants.OrganisationServiceField,
							Constants.OrganisationCodeField
						});
                        int nbSubDir = (from item in res.Cast<SPListItem>()
                                        where teamName.Equals(orgaList.GetItemListFieldValueAsText(organisationField, item), StringComparison.CurrentCultureIgnoreCase)
                                        select item.ID).Count();
                        foundSubDir = nbSubDir > 1 ? true : false;
                    }
                }
            }
            return foundSubDir;
        }
    }
    public static class SPListExtension
    {
        public static string GetItemListFieldValueAsText(this SPList list, string fieldName, SPListItem item)
        {
            SPFieldText textField = list.Fields[fieldName] as SPFieldText;
            return textField.GetFieldValueAsText(item[fieldName]);
        }

        public static SPListItemCollection GetSPListItemCollectionWithTextFields(this SPList list, List<string> fields)
        {
            StringBuilder sbViewFields = new StringBuilder();
            foreach (string fieldName in fields)
            {
                SPFieldText textField = list.Fields[fieldName] as SPFieldText;
                sbViewFields.AppendFormat(@"<FieldRef Name='{0}' />", textField.InternalName);
            }
            SPQuery query = new SPQuery()
            {
                ViewFields = sbViewFields.ToString()
            };
            return list.GetItems(query);
        }
    }
}

